package org.amphiprion.montrictracv4.fragment;

import java.util.List;

import org.amphiprion.montrictracv4.App;
import org.amphiprion.montrictracv4.MainActivity;
import org.amphiprion.montrictracv4.R;
import org.amphiprion.montrictracv4.db.adapter.SearchListAdapter;
import org.amphiprion.montrictracv4.db.dao.LudothequeDao;
import org.amphiprion.montrictracv4.db.dao.SearchDao;
import org.amphiprion.montrictracv4.db.model.Ludotheque;
import org.amphiprion.montrictracv4.db.model.Search;
import org.amphiprion.montrictracv4.swipe.SwipeDismissListViewTouchListener;
import org.amphiprion.montrictracv4.swipe.UndoBarController;
import org.amphiprion.montrictracv4.swipe.UndoBarController.UndoContent;
import org.amphiprion.montrictracv4.swipe.UndoBarController.UndoListener;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class SearchListFragment extends SherlockFragment implements TricTracFragment, UndoListener {
	private View fragment;
	private LayoutInflater mInflater;
	private SherlockFragmentActivity context;
	private ListView listView;
	private SearchListAdapter adapter;
	private UndoBarController undoBarController;

	public SearchListFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
		super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);

		this.context = getSherlockActivity();
		this.mInflater = paramLayoutInflater;
		this.fragment = paramLayoutInflater.inflate(R.layout.fragment_search_list, null);
		this.listView = ((ListView) this.fragment.findViewById(R.id.lstSearch));
		this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View arg1, int row, long arg3) {
				Search search = (Search) adapter.getItemAtPosition(row);
				selectLudothequeForSearch(search.getId());
			}
		});

		SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(listView, new SwipeDismissListViewTouchListener.OnDismissCallback() {
			@Override
			public void onDismiss(ListView listView, int[] reverseSortedPositions) {
				for (int position : reverseSortedPositions) {
					if (position < adapter.getCount()) {
						Search s = adapter.getItem(position);
						adapter.remove(s);
						undoBarController.showUndoBar(false, s.name, s, position);
					}
				}
				adapter.notifyDataSetChanged();
			}
		});
		listView.setOnTouchListener(touchListener);
		// Setting this scroll listener is required to ensure that during ListView scrolling,
		// we don't look for swipes.
		listView.setOnScrollListener(touchListener.makeScrollListener());

		undoBarController = new UndoBarController(this.fragment.findViewById(R.id.undobar), this);

		return this.fragment;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
	}

	public void initData() {
		List<Search> searches = SearchDao.getInstance(context).getAll();
		adapter = new SearchListAdapter(context, this, R.layout.search_item_row, searches);
		listView.setAdapter(adapter);
	}

	@Override
	public void menuOptionClicked(int id) {
		switch (id) {
		case R.id.action_add:
			App.currentSearch = null;
			addSearchItem();
			break;
		}
	}

	private void addSearchItem() {
		((MainActivity) getActivity()).showSearch(null);
	}

	@Override
	public void refresh() {
		initData();
	}

	public void editSearch(String searchId) {
		Search search = SearchDao.getInstance(context).getSearch(searchId);
		((MainActivity) getActivity()).showSearch(search);
	}

	private void selectLudothequeForSearch(String searchId) {
		final Search search = SearchDao.getInstance(context).getSearch(searchId);

		final List<Ludotheque> collections = LudothequeDao.getInstance(context).getLudoteques();
		if (collections.size() > 1) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(getResources().getString(R.string.ludoteque));
			builder.setAdapter(new ArrayAdapter<Ludotheque>(context, android.R.layout.select_dialog_item, collections), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int item) {
					dialog.dismiss();
					Ludotheque c = collections.get(item);
					App.currentSearch = search;
					MainActivity.sThis.showGameList(c);
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
		} else if (collections.size() == 1) {
			App.currentSearch = search;
			MainActivity.sThis.showGameList(collections.get(0));
		}
	}

	@Override
	public void onUndo(List<UndoContent> undoContents) {
		for (UndoContent uc : undoContents) {
			adapter.add(uc.position, (Search) uc.param);
		}
		adapter.notifyDataSetChanged();

	}

	@Override
	public void onNotUndo(List<UndoContent> undoContents) {
		for (UndoContent uc : undoContents) {
			SearchDao.getInstance(context).delete((Search) uc.param);
		}
	}
}
